﻿Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports CrystalDecisions.Web
Imports CrystalDecisions.ReportSource
Imports CrystalDecisions.CrystalReports
Imports System.IO
Imports Utilitario
Imports System.Net.Mime
' NOTA: puede usar el comando "Cambiar nombre" del menú contextual para cambiar el nombre de clase "EnvioEstadoCta" en el código, en svc y en el archivo de configuración a la vez.
Public Class EnvioEstadoCta
    Implements IEnvioEstadoCta

    Public Function enviarEmail(de As String, para As String, asunto As String, cc As String, cco As String, cuerpo As String, memoria As MemoryStream, tipContenido As ContentType) As Boolean Implements IEnvioEstadoCta.enviarEmail
        Dim valor As Boolean = False
        Dim servidor As New System.Net.Mail.SmtpClient

        Try
            Dim correo As New System.Net.Mail.MailMessage()
            correo.From = New System.Net.Mail.MailAddress(de)
            correo.Subject = asunto

            Dim archivoAdjunto As New System.Net.Mail.Attachment(memoria, tipContenido)
            correo.Attachments.Add(archivoAdjunto)

            correo.Body = cuerpo
            correo.To.Add(para)
            If cc <> "" Then
                correo.CC.Add(cc)
            End If
            If cco <> "" Then
                correo.Bcc.Add(cco)
            End If
            servidor.Host = "mail.gulda.com.pe"
            servidor.Port = 587
            servidor.EnableSsl = True
            servidor.UseDefaultCredentials = False
            servidor.Credentials = New System.Net.NetworkCredential("malarcon", "M@rkinsh0", "GLD")
            servidor.Send(correo)

            valor = True
        Catch ex As Exception
            valor = False
        End Try

        Return valor
    End Function

    Public Function generarPDF(rutaOrigen As String, rutaDestino As String, tipo As String,
                               ds As dsDetalle, codPedido As String, codCliente As String, nomCliente As String,
                               condicion As String, fecha As String, marca As String, moneda As String, agencia As String,
                               neto As Decimal, igv As Decimal, percepcion As Decimal, observaciones As String, direccion As String, ruc As String) As Boolean Implements IEnvioEstadoCta.generarPDF
        Dim valor As Boolean = False

        Try
            Dim rpt As New ReportDocument
            rpt.Load(rutaOrigen)
            rpt.SetDataSource(ds)
            If tipo = "P" Then
                rpt.SetParameterValue("@PEDIDO", codPedido)
                rpt.SetParameterValue("@Cliente", "(" & codCliente & ") " & nomCliente)
                rpt.SetParameterValue("@CondVenta", condicion)
                rpt.SetParameterValue("@Fecha", fecha)
                rpt.SetParameterValue("@Marca", IIf(marca Is Nothing, "", marca))
                rpt.SetParameterValue("@Moneda", moneda)
                rpt.SetParameterValue("@Agencia", agencia)
                rpt.SetParameterValue("@Observacion", observaciones)
                rpt.SetParameterValue("@Direccion", direccion)

                rpt.SetParameterValue("@Neto", Format(Val(neto), "##,##0.00"))
                rpt.SetParameterValue("@Igv", Format(Val(igv), "##,##0.00"))
                rpt.SetParameterValue("@Subtotal", Format(Val(neto + igv), "##,##0.00"))
                rpt.SetParameterValue("@Percepcion", Format(Val(percepcion), "##,##0.00"))
                rpt.SetParameterValue("@Total", Format(Val(CDec(neto) + CDec(igv) + CDec(percepcion)), "##,##0.00"))
            ElseIf tipo = "CO" Then
                Dim codigo As String = "0000000" & codPedido
                rpt.SetParameterValue("@Cotizacion", codigo.Substring(codigo.Length - 6, 6))
                rpt.SetParameterValue("@Fecha", FormatDateTime(Date.Now.ToString("dd/MM/yyyy"), DateFormat.LongDate))
                rpt.SetParameterValue("@Contacto", nomCliente)
                rpt.SetParameterValue("@Direccion", direccion)
                rpt.SetParameterValue("@Observacion1", observaciones)
                rpt.SetParameterValue("@Ruc", ruc)

                rpt.SetParameterValue("@Neto1", Format(Val(neto), "##,##0.00"))
                rpt.SetParameterValue("@Igv1", Format(Val(igv), "##,##0.00"))
                rpt.SetParameterValue("@Subtotal1", Format(Val(neto + igv), "##,##0.00"))
                rpt.SetParameterValue("@Percepcion1", Format(Val(percepcion), "##,##0.00"))
                rpt.SetParameterValue("@Total1", Format(Val(CDec(neto) + CDec(igv) + CDec(percepcion)), "##,##0.00"))
            End If
            Dim CrExportOptions As ExportOptions
            Dim CrDiskFileDestinationOptions As New  _
            DiskFileDestinationOptions()
            Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions()
            CrDiskFileDestinationOptions.DiskFileName = rutaDestino
            CrExportOptions = rpt.ExportOptions
            With CrExportOptions
                .ExportDestinationType = ExportDestinationType.DiskFile
                .ExportFormatType = ExportFormatType.PortableDocFormat
                .DestinationOptions = CrDiskFileDestinationOptions
                .FormatOptions = CrFormatTypeOptions
            End With
            rpt.Export()
            rpt.Close()
            valor = True
        Catch ex As Exception
            valor = False
        End Try

        Return valor
    End Function

    Public Function eliminarArchivo(ruta As String) As Boolean Implements IEnvioEstadoCta.eliminarArchivo
        Dim valor As Boolean = False

        Try
            My.Computer.FileSystem.DeleteFile(ruta)
            valor = True
        Catch ex As Exception
            valor = False
        End Try

        Return valor
    End Function

End Class
